<template>
    <div class="coupon_detail">
        <div class="coupon_info">
            <div class="left">
                <div class="left_top">
                    <div><span>{{ list.rate*100 }}</span>折</div>
                    <div class="top_info">
                        <div>{{ list.title }}</div>
                        <div>COUPON</div>
                    </div>
                </div>
            </div>
            <div class="receive">
                <van-button class="right_btn" type="primary" v-if="list.receive" @click="onCoupon">立即领取</van-button>
                <van-button class="right_btn" type="danger" disabled v-else>已领取</van-button>
            </div>
        </div>
        <div class="coupon_prompt">
            <div class="prompt_title">温馨提示：</div>
            <div><span>•</span>每人限领3张</div>
            <div><span>•</span>仅限量{{ list.total }}张，赶快领取！</div>
            <div><span>•</span>领取后{{ list.createtime_text }}至{{ list.endtime_text }}有效</div>
        </div>
    </div>

    <Tabbar />
</template>

<script setup>

import { onMounted } from 'vue';

    const {proxy} = getCurrentInstance()

    let business = proxy.$business;
    let cid = proxy.$route.query.cid ? proxy.$route.query.cid : 0;

    //优惠卷列表
    let list = ref([])

    let receive = ref([])

    onMounted(() => {
        CouponList()
    })

    const CouponList = async () => {
        var params = {
            busid:business.value.id,
            cid,
        }

        var result = await proxy.$POST({
            url:'coupon/info',
            params,
        })
        // console.log(result)
        // return false
        //没有更多数据
        if(result.code == 0)
        {
            proxy.$fail(result.msg)
            return false
        }
        list.value = result.data.coupon
        receive.value = result.data.receive
        // console.log(receive.value)
    }

    const onCoupon = async () => {
        var params = {
            busid:business.value.id,
            cid,
        }
        var result = await proxy.$POST({
            url:'coupon/receive',
            params,
        })

        // console.log(result)
        if(result.code == 0)
        {
            proxy.$fail(result.msg)
            return false
        }
        proxy.$success(result.msg)
        CouponList()
    }
</script>

<style scoped>
  @import url('/assets/css/CouponInfo.css');
</style>